package models

import (
	"time"

	"github.com/Qsnh/goa/utils"
	"github.com/astaxie/beego/orm"
)

type User struct {
	Id        int          `orm:"column(userid);PK" json:"userid"`
	Authlevel int          `orm:"column(authlevel)" json:"authlevel"`
	Avatar    string       `orm:"column(avatar)" json:"avatar"`
	Level     int          `orm:"column(level)" json:"level"`
	Mobile    string       `orm:"column(mobile)" json:"mobile"`
	Nickname  string       `orm:"column(name)" json:"name"`
	Password  string       `orm:"column(password)" json:"-"`
	Ut        time.Time    `orm:"column(ut)" json:"ut"`
	Questions []*Questions `orm:"reverse(many)" json:"questions"`
	Answers   []*Answers   `orm:"reverse(many)" json:"answers"`
}

func FindFlaskUserById(id int) (*User, error) {
	user := new(User)
	db := orm.NewOrm()
	err := db.QueryTable(user).Filter("userid", id).One(user)
	return user, err
}

func UserExistsByMobileAndPassword(mobile string, password string) (*User, error) {
	user := new(User)
	db := orm.NewOrm()
	err := db.QueryTable(user).Filter("mobile", mobile).Filter("password", utils.MD5Encode(password)).One(user)
	if err != nil {
		return nil, err
	}
	return user, nil
}
